package com.webex.ta.hydra.gui.table;

import com.webex.ta.hydra.Constants;
import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.gui.MenuFactory;

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.*;
import java.awt.event.MouseEvent;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-25
 * Time: 16:05:06
 */
public class HydraTableMouseHandler extends MouseInputAdapter {

    private HydraTable table;

    public HydraTableMouseHandler(HydraTable table) {
        this.table = table;
    }

    @Override
    public void mousePressed(MouseEvent evt) {
        if (table.getSelectedRowCount() <= 1 && evt.getModifiers() == Event.META_MASK)
            processEvent(evt);
        Point p = evt.getPoint();
        if (evt.getSource() != table) {
            p.x -= table.getX();
            p.y -= table.getY();
        }

        int row = table.rowAtPoint(p);
        int column = table.columnAtPoint(p);
        if (column == Constants.COLUMN_KEYWORD && row != -1) {
            Command command = table.getModel().getCommandAt(row);
            if (ExpansionToggleBorder
                    .isExpansionToggle(command.getIndent(), p.x - 20)) {
                //TODO: replace 20 with the width of breakpoint column
                table.toggleExpanded(row);
                evt.consume();
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3 && e.getClickCount() < 2) {
            Point p = e.getPoint();
            if (e.getSource() != table) {
                p.x -= table.getX();
                p.y -= table.getY();
            }

//            int row = e.getY() / table.getRowHeight();
            if (table.isEditing()) {
                table.getCellEditor().stopCellEditing();
            }

            JPopupMenu contextMenu = MenuFactory.createContextMenu();
            contextMenu.show((Component) e.getSource(), p.x, p.y);
        }
    }

    @Override
    public void mouseMoved(MouseEvent evt) {
        Point p = evt.getPoint();
        if (evt.getSource() != table) {
            p.x -= table.getX();
            p.y -= table.getY();
        }

        int row = table.rowAtPoint(p);
        int column = table.columnAtPoint(p);
        if (row >= 0) {
            Command cmd = table.getModel().getCommandAt(row);
            if (!table.isAnnotationEditing()) {
                if (column == Constants.COLUMN_KEYWORD  &&cmd.hasAnnotation())
                    table.showAnnotationAt(row);
                else
                    table.hideAnnotation();
            }
        }
    }

    private void processEvent(MouseEvent e) {
        if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
            int modifiers = e.getModifiers();
            modifiers -= MouseEvent.BUTTON3_MASK;
            modifiers |= MouseEvent.BUTTON1_MASK;
            MouseEvent ne = new MouseEvent(e.getComponent(), e.getID(),
                    e.getWhen(), modifiers, e.getX(), e
                            .getY(), e.getClickCount(), false);
            table.dispatchEvent(ne);
        }
    }
}
